


function getParamValue(urlstring, paramname) {
    var paramvalue = "";
    var splits = urlstring.split('/');
    for (i = 0; i < splits.length; i++) {
        if(splits[i] == paramname) {
            paramvalue = splits[i+1];
        }
    }
    return paramvalue;
}

function updateTips(placeholder, text) {
    placeholder
        .text( text )
        .addClass( "ui-state-highlight" );
    setTimeout(function() {
        placeholder.removeClass( "ui-state-highlight", 1500 );
    }, 500 );
}

function checkLength( o, n, min, max ) {
    if ( o.val().length > max || o.val().length < min ) {
        o.addClass( "ui-state-error" );
        updateTips( "Length of " + n + " must be between " +
            min + " and " + max + "." );
        return false;
    } else {
        return true;
    }
}

function checkRegexp( o, regexp, n ) {
    if ( !( regexp.test( o.val() ) ) ) {
        o.addClass( "ui-state-error" );
        updateTips( n );
        return false;
    } else {
        return true;
    }
}

function loadExtendedUnits(value, selected, target) {
    if ("all" == value) {
        $.ajax({
            type: 'get',
            url: "/data/searchUnit",
            dataType: "html",
            success: function(data, textStatus, jqXHR) {
                $('#unit_unit').clearOptions();
                $('#unit_unit').buildOptions(jQuery.parseJSON(data), selected);
                return false;
            },
            error: function(jqXHR, textStatus, errorThrown) {
                alert("error: " + textStatus + ", " + errorThrown);
                return false;
            }
        });

        // enable add unit button
        $("#add_unit").show();
    }
}

function showUnitForm() {
    var dialogOptions = {
        closeOnEscape: true,
        position: 'center',
        width: '330px',
        buttons: {
            'Save': function() {
                insertUnit();
            },
            'Close': function() {
                $(this).dialog("close");
            }
        },
        close: function() {
        }
    };
    
    if ($("#unit_form").exists()) {
        $("#unit_form_msg").html("").hide();
        $("#unit_new").val("");
        $("#unit_form").dialog(dialogOptions);
        return false;
    }
    
    var dialog = $("<div title='Add new unit' style='display:hidden'></div>").appendTo('body');
    dialog.load(
        "/data/newUnitForm",
        {},
        function(responseText, textStatus, XMLHttpRequest){
            dialog.dialog(dialogOptions);
        }
    );
    return false;
}

function insertUnit() {
    var unit = $("#unit_new").val();
    var mesg = "";
    
    //validate
    if (unit == ""){
        mesg = "Unit value cannot be empty";
        $("#unit_form_msg").html(mesg).show();
        return;
    }
    
    $.post(
        '/data/createUnit',
        {unit: unit},
        function(data) {
            if (data == 'false') {
                mesg = "Unit already exists";
                $("#unit_form_msg").html(mesg).show();
            }
            else {
                // reload
                loadExtendedUnits('all', unit);
                $("#unit_form").parents('.ui-dialog').hide();
            }
        }
    );
}
